package service

import (
	"ksd-social-api/commons/base/service"
	"ksd-social-api/modules/note/mapper"
	"ksd-social-api/modules/note/model"
	"ksd-social-api/modules/note/model/context"
	"ksd-social-api/modules/note/model/vo"
	"ksd-social-api/utils"
	"strconv"
	"time"
)

type NoteChapterService struct {
	service.BaseService
}

var noteChapterMapper = mapper.NoteChapterMapper{}
var noteLessonService = NoteLessonService{}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询章信息
 */
func (service *NoteChapterService) FindChapterLessonsStatus(noteId uint64, systemId uint64) ([]*vo.NoteChapterVo, error) {
	var noteChapterVos []*vo.NoteChapterVo
	chapterList, _ := noteChapterMapper.FindChapterLessonsStatus(noteId, systemId)
	for _, chapter := range chapterList {
		var noteChapterVo vo.NoteChapterVo
		utils.CopyProperties(&noteChapterVo, chapter)
		noteChapterVo.Id = strconv.FormatUint(chapter.Id, 10)
		noteChapterVo.NoteId = strconv.FormatUint(chapter.NoteId, 10)
		lessonsList, _ := noteLessonService.FindLessonsStatus(noteId, chapter.Id, systemId)
		noteChapterVo.Expand = true
		if len(lessonsList) == 0 {
			noteChapterVo.Children = []*vo.NoteLessonVo{}
		} else {
			noteChapterVo.Children = lessonsList
		}
		noteChapterVos = append(noteChapterVos, &noteChapterVo)
	}
	return noteChapterVos, nil
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询章信息
 */
func (service *NoteChapterService) FindChapterLessons(noteId uint64, systemId uint64) ([]*vo.NoteChapterVo, error) {
	var noteChapterVos []*vo.NoteChapterVo
	chapterList, _ := noteChapterMapper.FindChapterLessons(noteId, systemId)
	for _, chapter := range chapterList {
		var noteChapterVo vo.NoteChapterVo
		utils.CopyProperties(&noteChapterVo, chapter)
		noteChapterVo.Id = strconv.FormatUint(chapter.Id, 10)
		noteChapterVo.NoteId = strconv.FormatUint(chapter.NoteId, 10)
		lessonsList, _ := noteLessonService.FindLessons(noteId, chapter.Id, systemId)
		noteChapterVo.Expand = true
		if len(lessonsList) == 0 {
			noteChapterVo.Children = []*vo.NoteLessonVo{}
		} else {
			noteChapterVo.Children = lessonsList
		}
		noteChapterVos = append(noteChapterVos, &noteChapterVo)
	}
	return noteChapterVos, nil
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询章信息
 */
func (service *NoteChapterService) GetById(id uint64, systemId uint64) (noteChapter *vo.NoteChapterVo, err error) {
	chapter, _ := noteChapterMapper.GetById(id, systemId)
	noteChapterVo := vo.NoteChapterVo{}
	utils.CopyProperties(&noteChapterVo, chapter)
	noteChapterVo.Id = strconv.FormatUint(chapter.Id, 10)
	noteChapterVo.NoteId = strconv.FormatUint(chapter.NoteId, 10)
	return &noteChapterVo, nil
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 保存章章信息
 */
func (service *NoteChapterService) SaveChapter(ctx context.NoteChapterContext) (chapterVo *vo.NoteChapterVo, err error) {
	noteChapter := model.NoteChapter{}
	utils.CopyProperties(&noteChapter, ctx)
	NoteId, _ := strconv.ParseUint(ctx.NoteId, 10, 64)
	noteChapter.NoteId = NoteId
	noteChapter.Status = 1
	noteChapter.FreeFlag = 1
	chapter, _ := noteChapterMapper.SaveChapter(&noteChapter)
	noteChapterVo := vo.NoteChapterVo{}
	utils.CopyProperties(&noteChapterVo, chapter)
	noteChapterVo.Id = strconv.FormatUint(chapter.Id, 10)
	noteChapterVo.NoteId = strconv.FormatUint(chapter.NoteId, 10)
	return &noteChapterVo, nil
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 复制章章信息
 */
func (service *NoteChapterService) CopyChapter(id uint64, systemId uint64) (chapterVo *vo.NoteChapterVo, err error) {
	newChapter, _ := noteChapterMapper.GetById(id, systemId)
	newChapter.Id = 0
	newChapter.Status = 1
	newChapter.Title = newChapter.Title + "[副本]"
	newChapter.CreateTime = time.Now()
	chapter, err := noteChapterMapper.SaveChapter(newChapter)
	noteChapterVo := vo.NoteChapterVo{}
	utils.CopyProperties(&noteChapterVo, chapter)
	noteChapterVo.Id = strconv.FormatUint(chapter.Id, 10)
	noteChapterVo.NoteId = strconv.FormatUint(chapter.NoteId, 10)
	return &noteChapterVo, nil
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id修改章信息
 */
func (service *NoteChapterService) UpdateById(ctx context.NoteChapterStatusContext) (flag bool, err error) {
	ID, _ := strconv.ParseUint(ctx.Id, 10, 64)
	return noteChapterMapper.UpdateById(ctx.Field, ctx.Value, ID)
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id修改章信息
 */
func (service *NoteChapterService) UpdateByIdContent(ctx context.NoteChapterContentContext) (flag bool, err error) {
	NoteId, _ := strconv.ParseUint(ctx.NoteId, 10, 64)
	ID, _ := strconv.ParseUint(ctx.Id, 10, 64)
	return noteChapterMapper.UpdateByIdContent(ctx.Mdcontent, ctx.Htmlcontent, ctx.SystemId, NoteId, ID)
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id修改章信息
 */
func (service *NoteChapterService) RemoveByID(ctx context.NoteChapterRemoveContext) (flag bool) {
	NoteId, _ := strconv.ParseUint(ctx.NoteId, 10, 64)
	ID, _ := strconv.ParseUint(ctx.Id, 10, 64)
	return noteChapterMapper.RemoveById(ID, NoteId, ctx.SystemId)
}
